package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-09-05 19:51
 **/
@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;

/*    @GetMapping("/emp")
    public Result queryEmp(Integer page, Integer pageSize) {
        PageResult<Emp> pageResult = empService.queryEmpList(page, pageSize);
        return Result.success(pageResult);
    }*/

    /**
     * 分页查询+条件查询
     *
     * @param
     * @return
     */
   /* @GetMapping
    public Result conditionQueryEmpList(
            Integer page,
            Integer pageSize,
            String name,
            Integer gender,
            Integer job,
            @DateTimeFormat(pattern = "yyyy-dd-MM") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-dd-MM") LocalDate end

    ) {
        PageResult<Emp> pageResult = empService.conditionQueryEmpList(page, pageSize, name, gender, job, begin, end);
        return Result.success(pageResult);
    }*/

    /**
     * 分页查询+条件查询封装实体类
     * @param empQueryParam 条件查询封装实体类
     * @return
     */
    @GetMapping
    public Result conditionQueryEmpList(EmpQueryParam empQueryParam) {
        PageResult<Emp> pageResult = empService.conditionQueryEmpList(empQueryParam);

        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result queryEmpById(@PathVariable Integer id) {
        log.info("查询id为{}的员工", id);
//        Emp emp = empService.queryEmpById(id);
        Emp emp = empService.queryEmpAndExpr(id);
        return Result.success(emp);
    }
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
//        empService.addEmp(emp);
        empService.addEmpAndExpr(emp);
        return Result.success();
    }
    @DeleteMapping()
    public Result delEmpById(@RequestParam List<Integer> ids) {
        log.info("删除id为{}的员工", ids);
        Integer num = empService.delEmpById(ids);
        if (num >= 1) {
            return Result.success("删除成功！");
        } else
            return Result.error("删除失败！");
    }
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
//        empService.updateEmp(emp);
        empService.updateEmpAndExpr(emp);
        return Result.success();
    }
    @GetMapping("/list")
    public Result queryEmpList(){
       List<Emp>list = empService.list();
       return Result.success(list);
    }
}
